﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using Advisor.Entities;
using System.Collections;
using NHibernate.Criterion;

namespace Advisor.Entities
{
   public class RiskDAO
    {
       private Users m_User;
       public RiskDAO(Users user)
       {
           this.m_User = user;
       }

        /// <summary>
        /// Retorna todas os registros da tabela Risk. 
        /// </summary>
        /// <param name="column"></param>
        /// <param name="maximumRows"></param>
        /// <param name="startRowIndex"></param>
        /// <returns></returns>
        internal  IList GetRisks(FilterCollection filters)
        {
            ISession session = NHibernateSession.GetSession();
            ICriteria c = session.CreateCriteria(typeof(Risk));

            foreach (ICriterion filter in filters)
            {
                c.Add(filter);
            }
            return c.List();
        }
        /// <summary>
        ///  Método para inserir um Risk no banco. 
        /// </summary>
        /// <param name="history"></param>
        internal void InsertRisk(Risk risk)
        {
            ISession session = NHibernateSession.GetSession();
            session.Save(risk);
            session.Close();
        }
        /// <summary>
        /// Retorna uma instância da tabela Risk passando o id;
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        internal Risk GetRisk(FilterCollection filters)
        {
            ISession session = NHibernateSession.GetSession();
            ICriteria c = session.CreateCriteria(typeof(Risk));

            foreach (ICriterion filter in filters)
            {
                c.Add(filter);
            }
            return c.UniqueResult<Risk>();
        }
        /// <summary>
        /// Remove um Risk do banco
        /// </summary>
        /// <param name="history"></param>
        internal void DeleteRisk(Risk risk)
        {
            ISession sessions = NHibernateSession.GetSession();
            sessions.Delete(risk);
            sessions.Close();
        }

        /// <summary>
        /// Atualiza um Risk do banco. 
        /// </summary>
        /// <param name="history"></param>
        internal void UpdateRisk(Risk risk)
        {
            ISession session = NHibernateSession.GetSession();
            session.Update(risk);
            //Comando que ativa as alterações no banco de dados. 
            session.Flush();
            session.Close();
        }

    }
    }

